package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.LoginAuthBean;
import com.ymatou.iapi.login.service.LoginAuthCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.LockUserRequestDtoBean;
import com.ymatou.iapi.userservice.service.*;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
/**
*锁定用户查询接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserLockStatusRequestDto{
	private static UserLockStatusRequestDtoCall userlockstatusrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("锁定用户查询接口");
	}

	@Before
	public void caseUp() {
		userlockstatusrequestdtoCall=new UserLockStatusRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userlockstatusrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserLockStatusRequestDto_001() {
		Logger.start(true,"锁定用户后查询");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId = urrc.getUserid();
			String ip = regbean.getClientIp();
			String UserName =regbean.getLoginId();
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(UserId);
			//锁定用户
			Logger.comment("锁定用户");
			lockNewUser(users);
			//登录验证
			Logger.comment("开始登陆，验证用户被锁定");
			verifyLogin(UserName,ip);	
			//查询
			Logger.comment("开始查询用户的锁定状态");
			userlockstatusrequestdtoCall.setData("UserIdString="+UserId);
			
			//由于数据库log是异步写入的，无法及时取回同步的数据，此方法的作用是为了等待数据同步获取正确的数据
			boolean flag=true;
			int redo=0;
			while(flag){
				userlockstatusrequestdtoCall.setData("UserIdString="+UserId);
				userlockstatusrequestdtoCall.callService();
				if(userlockstatusrequestdtoCall.getResult().toString().split(",").length>=3){
					flag=false;
					break;
				}
				Thread.sleep(1000);
				if(redo>3){
					flag=false;
				}
				redo++;
			}
			Logger.verifyEquals("True", userlockstatusrequestdtoCall.getResult().get(String.valueOf(UserId)).toString().split(",")[0], "验证改用户为已锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserLockStatusRequestDto_002() {
		Logger.start(true,"查询多个用户的锁定状态");
		try {
			StringBuilder userIdString = new StringBuilder();
			ArrayList<Integer> userIds = new ArrayList<Integer>();
			ArrayList<String> ips = new ArrayList<String>();
			//锁定用户
			Logger.comment("锁定用户及验证登陆");
			for(int i = 0; i<2; i++){
				UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
				UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
				JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
				String AccessToken=registerResult.get("Token").getAsString();
				int UserId = urrc.getUserid();
				String ip = regbean.getClientIp();
				String UserName =regbean.getLoginId();
				ArrayList<Integer> users=new ArrayList<Integer>();
				users.add(UserId);
				userIds.add(UserId);
				ips.add(ip);
				//锁定用户
				lockNewUser(users);
				//登录验证
				verifyLogin(UserName,ip);
				userIdString.append(UserId+",");
			}
			userIdString.append(",,");
			//查询
			Logger.comment("开始查询用户的锁定状态");
			userlockstatusrequestdtoCall.setData("UserIdString="+userIdString);
			userlockstatusrequestdtoCall.callService();	
			for(int i =0; i<userIds.size(); i++){
				String status = userlockstatusrequestdtoCall.getResult().get(String.valueOf(userIds.get(i))).toString();
				Logger.verifyEquals("True", status.split(",")[0], "验证改用户为已锁定状态");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserLockStatusRequestDto_003() {
		Logger.start(true,"解锁后用户查询");
		try {
			//锁定用户
			LockUserRequestDtoBean lockuserrequestdtoBean=new LockUserRequestDtoBean();
			LockUserRequestDtoCall lockuserrequestdtoCall=new LockUserRequestDtoCall();
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegSource(0);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId = urrc.getUserid();
			String ip = regbean.getClientIp();
			String UserName =regbean.getLoginId();
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(UserId);
			Logger.comment("锁定用户");
			lockuserrequestdtoBean.setUserId(users);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			//登录验证
			Logger.comment("开始登陆，验证用户被锁定");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(UserName);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lab.setSourceType("mainsite");
			lab.setClientIp(ip);
			lac.setData(false,lab);
			lac.callService();
			Logger.verifyIsContains("用户被锁定", lac.getMsg(), "验证登录返回消息");		
			//查询
			Logger.comment("开始查询用户的锁定状态");
			userlockstatusrequestdtoCall.setData("UserIdString="+UserId);
			userlockstatusrequestdtoCall.callService();			
			Logger.comment("userlockstatusrequestdtoCall send:" + userlockstatusrequestdtoCall.getEntityString());
			Logger.comment("userlockstatusrequestdtoCall response:" + userlockstatusrequestdtoCall.getReturnData());
			Logger.verifyEquals("True", userlockstatusrequestdtoCall.getResult().get(String.valueOf(UserId)).toString().split(",")[0], "验证改用户为已锁定状态");
			//解锁
			Logger.comment("解锁用户");
			UnLockUserRequestDtoCall unlockuserrequestdtoCall=new UnLockUserRequestDtoCall();
			unlockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			unlockuserrequestdtoCall.callService();
			Thread.sleep(2000);
			//登录验证
			
			lac=JUserCallService.login(UserName, JUserCallService.defaultLoginPwd, "mainsite", ip, regbean.getDeviceId());
			Logger.verifyEquals(true, lac.getSuccess(), "验证登录成功");
			//查询状态
			Logger.comment("验证用户的锁定状态");
			userlockstatusrequestdtoCall.setData("UserIdString="+UserId);
			userlockstatusrequestdtoCall.callService();			
			Logger.verifyIsContains("False", userlockstatusrequestdtoCall.getResult().get(String.valueOf(UserId)).toString().split(",")[0], "验证用户状态解锁");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserLockStatusRequestDto_004() {
		Logger.start(false,"用户id为空");
		try {
			//查询条件错误
			userlockstatusrequestdtoCall.setData("UserIdString=");
			userlockstatusrequestdtoCall.callService();			
			Logger.verifyEquals("400",userlockstatusrequestdtoCall.getString("ResponseCode") , "验证ResponseCode");
			Logger.verifyEquals("true",userlockstatusrequestdtoCall.getString("Success") , "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 锁定一个新用户
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private LockUserRequestDtoBean lockNewUser(ArrayList<Integer> users) throws ClientProtocolException, IOException{
		LockUserRequestDtoBean lockuserrequestdtoBean=new LockUserRequestDtoBean();
		LockUserRequestDtoCall lockuserrequestdtoCall=new LockUserRequestDtoCall();
		lockuserrequestdtoBean.setUserId(users);
		lockuserrequestdtoBean.setDescription("autotest");
		lockuserrequestdtoBean.setOperating("auto");
		lockuserrequestdtoCall.setData(false, lockuserrequestdtoBean);
		lockuserrequestdtoCall.callService();
		return lockuserrequestdtoBean;
	}
	
	/**
	 * 用户登陆认证
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private void verifyLogin(String loginId,String ip) throws ClientProtocolException, IOException{
		//登录验证
		LoginAuthBean lab = new LoginAuthBean();
		LoginAuthCall lac = new LoginAuthCall();
		lab.setLoginId(loginId);
		lab.setPas(JUserCallService.defaultLoginPwd);
		lab.setSourceType("mainsite");
		lab.setClientIp(ip);
		lac.setData(false,lab);
		lac.callService();
		Logger.verifyIsContains("用户被锁定", lac.getMsg(), "验证登录返回消息");	
	}
}